﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.RAL.Access;
using Empires.CRL.DataContract;
using Empires.RAL.ORM.CompanyData;

namespace Empires.RAL.Pump
{
    public class CompanyNewsDataPump : ICompanyNewsAccess
    {
        public List<DefaultDataPack> GetNewsList(string copName, string type, string subject, int page, int size)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var queryable = (from c in ctx.tb_news
                                 where c.c_vc_cpname == copName && !c.c_b_isdelete
                                 orderby c.c_i_id descending
                                 select new
                                 {
                                     c.c_i_type,
                                     c.c_i_subject,
                                     c.c_nvc_title,
                                     c.c_dt_pbdate,
                                     c.c_i_hit,
                                     c.c_i_id,
                                     c.c_nvc_author
                                 })
                              .Skip(page * size)
                              .Take(size);
                if (!String.IsNullOrEmpty(type))
                {
                    queryable = queryable.Where(c => c.c_i_type == Int32.Parse(type));
                }
                if (!String.IsNullOrEmpty(subject))
                {
                    queryable = queryable.Where(c => c.c_i_subject == Int32.Parse(subject));
                }
                var result = queryable.ToList()
                              .Select(c => new DefaultDataPack()
                              {
                                  TITLE = c.c_nvc_title,
                                  AUTHOR = c.c_nvc_author,
                                  DATE = c.c_dt_pbdate.ToShortDateString(),
                                  HIT = c.c_i_hit,
                                  ID = c.c_i_id
                              })
                              .ToList();
                return result;
            }
        }
        public int GetNewsItemCount(string copName, string type, string subject)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = from c in ctx.tb_news
                             where c.c_vc_cpname == copName && !c.c_b_isdelete
                             select
                             new
                             {
                                 c.c_i_id,
                                 c.c_i_type,
                                 c.c_i_subject
                             };
                if (!String.IsNullOrEmpty(type))
                {
                    result = result.Where(c => c.c_i_type == Int32.Parse(type));
                }
                if (!String.IsNullOrEmpty(subject))
                {
                    result = result.Where(c => c.c_i_subject == Int32.Parse(subject));
                }
                return result.Count();
            }
        }
        public List<DefaultDataPack> GetNewsDetail(int nid)
        {
            List<DefaultDataPack> list = new List<DefaultDataPack>();
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_news
                              where c.c_i_id == nid && !c.c_b_isdelete
                              select new
                              {
                                  c.c_nvc_title,
                                  c.c_nvc_author,
                                  c.c_dt_pbdate,
                                  c.c_i_hit,
                                  c.c_nvc_content,
                                  c.c_nvc_from
                              }).FirstOrDefault();
                if (result != null)
                {
                    list.Add(new DefaultDataPack()
                    {
                        TITLE = result.c_nvc_title,
                        AUTHOR = result.c_nvc_author,
                        DATE = result.c_dt_pbdate.ToShortDateString(),
                        HIT = result.c_i_hit,
                        CONTENT = result.c_nvc_content,
                        FROM = result.c_nvc_from
                    });
                }
            }
            return list;
        }
        public int DeleteNewsItem(string copName, int id)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var item = ctx.tb_news.Where(c => c.c_i_id == id && c.c_vc_cpname == copName).SingleOrDefault();
                if (item != null)
                {
                    item.c_b_isdelete = true;
                    return ctx.SaveChanges();
                }
                return 0;
            }
        }

        public int AddNewsItem(string cpname,string author, string content, string from, string title)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                ctx.tb_news.AddObject(
                    new tb_news() { 
                        c_b_isdelete = false,
                        c_b_istop=false,
                        c_dt_pbdate=DateTime.Now,
                        c_i_hit=10,
                        c_nvc_author=author,
                        c_nvc_content=content,
                        c_nvc_from=from,
                        c_nvc_title=title,
                        c_vc_cpname=cpname
                    });
                return ctx.SaveChanges();
            }
        }
    }
}
